package com.yunbao.hujin.mychoosealbum;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yunbao.hujin.mychoosealbum.adapter.AdapterShowChooseImg2;
import com.yunbao.hujin.mychoosealbum.utils.Const;
import com.yunbao.hujin.mychoosealbum.utils.GlideUtil;

import java.util.ArrayList;

public class ChooseImgShow2Activity extends AppCompatActivity {

    private ViewPager viewPager;
    private TextView mIndictorView;
    private ImageView back;

    private ArrayList<String> mPhotoLists;
    private int mLenght;
    private int position;


    private AdapterShowChooseImg2 adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_img_show2);

        initView();
        initListener();
    }

    private void initView() {
        //必须在前
        initData();

        mIndictorView = (TextView) findViewById(R.id.indictor_view);
        viewPager = (ViewPager) findViewById(R.id.photo_pager);
        back = findViewById(R.id.back);

        mIndictorView.setText((position + 1) + "/" + mLenght);

        adapter = new AdapterShowChooseImg2(this, mPhotoLists);
//        viewPager.setPageMargin(DisplayUtil.dip2px(this, 30));
        viewPager.setPageMargin(30);    //浏览图片时让图片之间有空隙
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(position);

        //隐藏软键盘
        hideSoftInputMethod(this, mIndictorView);
    }

    /**
     * 初始化获取到的数据
     */
    private void initData() {
//        mPhotoLists = getIntent().getStringArrayListExtra(Const.GOTO_FOR_SHOW_IMG);
        mPhotoLists = new ArrayList<>();
        if (ChooseImgAllActivity.instance != null) {
            mPhotoLists.addAll(ChooseImgAllActivity.instance.gridList);
        }
        position = getIntent().getIntExtra(Const.GOTO_FOR_SHOW_IMG_POSITION, 0);

        if (mPhotoLists.get(0).equals(Const.PHOTO)) {    //调用本地相机
            mPhotoLists.remove(0);
            position -= 1;
        }
        mLenght = mPhotoLists.size();
    }

    private void initListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mIndictorView.setText(String.valueOf((position + 1))
                        .concat("/").concat(String.valueOf(mLenght)));
                //                currentPos = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                position = viewPager.getCurrentItem();
            }
        });
    }


    /**
     * 隐藏软键盘
     */
    private void hideSoftInputMethod(Context context, View view) {
        InputMethodManager manager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        setContentView(R.layout.view_null);
        GlideUtil.clearGlideMemory(this);
    }

}
